package com.lumeng.spring.common.utils;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能描述:
 * <日志处理类>
 *
 * @ClassName: LogUtils
 * @Author: wanghui
 * @Date: 2019年12月20日 22:25:00
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
public class LogUtils {

    static Logger getLogger(final Class cla){
        Logger logger = LoggerFactory.getLogger(cla);
        return logger;
    }

    static String getStrings(final String... strings){
        StringBuffer sb = new StringBuffer();
        for(String str : strings){
            sb.append(str+"-");
        }
        int len = sb.toString().length();
        return len >=1 ? sb.toString().substring(0,len-1) : sb.toString();
    }

    public static void debug(final Class cla,final String msg){
        getLogger(cla).debug(getCommonLoginfo(cla,msg));
    }
    public static void debug(final Class cla,final String format,final Object... arguments){
        getLogger(cla).debug(getCommonLoginfo(cla,format),arguments);
    }

    public static void info(final Class cla,final String msg){
        getLogger(cla).info(getCommonLoginfo(cla,msg));
    }

    public static void info(final Class cla,final String format,final Object... arguments){
        getLogger(cla).info(getCommonLoginfo(cla,format),arguments);
    }

    public static void info(final Class cla,final Exception ex,final String... strings){
        getLogger(cla).info(getCommonLoginfo(cla,getStrings(strings)), ex);
    }

    public static void error(final Class cla,final String... strings){
        getLogger(cla).error(getCommonLoginfo(cla,getStrings(strings)));
    }

    public static void error(final Class cla,final Throwable t,final String... strings){
        getLogger(cla).error(getCommonLoginfo(cla,getStrings(strings)),t);
    }

    public static void error(final Class cla,final String msg,final Throwable t){
        getLogger(cla).error(getCommonLoginfo(cla,msg),t);
    }

    public static void error(final Class cla,final String format,final Object... arguments){
        getLogger(cla).error(getCommonLoginfo(cla,format),arguments);
    }

    public static String getCommonLoginfo(final Class cla,final String msg){
        StringBuilder stringBuilder = new StringBuilder();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String remoteIp = IpAdrressUtil.getRemoteIpAdrress(request);
        String localIp = IpAdrressUtil.getLocalHostAddress();
        Object traceId = request.getAttribute("traceId");
        if (traceId!=null)
            stringBuilder.append("[").append(traceId).append("]");
        if (StringUtils.isNotEmpty(remoteIp))
            stringBuilder.append(",目标IP:").append(remoteIp);
        if (StringUtils.isNotEmpty(localIp))
            stringBuilder.append(",本机IP:").append(localIp);
        stringBuilder.append(",当前对象类class:").append(cla.getSimpleName());

        stringBuilder.append("-->{");
        stringBuilder.append("\n    "+msg);
        stringBuilder.append("\n}");

        return stringBuilder.toString();
    }
}
